﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RPSImporter.Infrastructure;
using RPSImporter.Services.Data;
using System.Globalization;
using RPSImporter.Infrastructure.Attributes;
using RPSImporter.Models.Settings;
using RPSImporter.Services;

namespace RPSImporter.Models
{
    [FileTypeName("PersonHours_FileType")]
    public class HoursPerson : ModelBase, IModel
    {
        public HoursPerson()
        {
            this.FileSettings = DatabaseSvc.Instance.Settings.HoursPersonFileSettings.First();
        }

        public HoursPersonFileSettings FileSettings { get; set; }

        public bool Import()
        {
            try
            {
                if (this.GetData(this.FileSettings.NrHours) != String.Empty
                   && !this.DB.hts.Any(h => h.year == this.GetData(this.FileSettings.Year)
                                         && h.month == this.GetData(this.FileSettings.Month)
                                         && h.PERSON.SOCIAL_SECURITY_NUMBER == this.GetData(this.FileSettings.NrSecuriteSociale)
                                         && h.PERSON.ControlKey == this.GetData(this.FileSettings.KeyNIR)))
                {
                    ht hour = new ht();
                    hour.year = this.GetData(this.FileSettings.Year);
                    hour.month = this.GetMonthNumber(this.GetData(this.FileSettings.Month)).ToString();
                    hour.nr_hours = (this.GetData(this.FileSettings.NrHours) != String.Empty) ?
                                      Convert.ToDouble(this.GetData(this.FileSettings.NrHours)) : (double?)null;
                    if (this.GetData(this.FileSettings.NrSecuriteSociale) == String.Empty)
                    {
                        hour.person_id = (from p in this.DB.PERSONs
                                          where p.BADGE_NUMBER == this.GetData(this.FileSettings.BadgeNumber).TrimStart('0')
                                          select p.PERSON_ID).FirstOrDefault();
                    }
                    else
                    {
                        hour.person_id = (from p in this.DB.PERSONs
                                          where p.SOCIAL_SECURITY_NUMBER == this.GetData(this.FileSettings.NrSecuriteSociale).NoWhiteSpace() &&
                                                p.ControlKey == this.GetData(this.FileSettings.KeyNIR).TrimStart('0')
                                          select p.PERSON_ID).FirstOrDefault();
                    }
                    //if (hour.person_id != 0)
                    {
                        this.DB.hts.InsertOnSubmit(hour);
                        this.DB.SubmitChanges();
                        this.UpdateHierarchyHours(hour, hour.nr_hours);
                    }
                }
                else
                {
                    ht hour = this.DB.hts.FirstOrDefault(h => h.year == this.GetData(this.FileSettings.Year)
                                         && h.month == this.GetData(this.FileSettings.Month)
                                         && h.PERSON.SOCIAL_SECURITY_NUMBER == this.GetData(this.FileSettings.NrSecuriteSociale)
                                         && h.PERSON.ControlKey == this.GetData(this.FileSettings.KeyNIR));
                    double oldHours = 0;
                    if (hour.nr_hours != null)
                    {
                        oldHours = (double)hour.nr_hours;
                    }

                    hour.year = this.GetData(this.FileSettings.Year);
                    hour.month = this.GetMonthNumber(this.GetData(this.FileSettings.Month)).ToString();
                    hour.nr_hours = (this.GetData(this.FileSettings.NrHours) != String.Empty) ?
                                      Convert.ToDouble(this.GetData(this.FileSettings.NrHours)) : (double?)null;
                    hour.person_id = (from p in this.DB.PERSONs
                                      where p.SOCIAL_SECURITY_NUMBER == this.GetData(this.FileSettings.NrSecuriteSociale).NoWhiteSpace() &&
                                            p.ControlKey == this.GetData(this.FileSettings.KeyNIR)
                                      select p.PERSON_ID).FirstOrDefault();
                    this.DB.SubmitChanges();
                    this.UpdateHierarchyHours(hour, hour.nr_hours - oldHours);
                }
                return true;
            }
            catch (Exception ex)
            {
                this.Error = ex.Message;
                return false;
            }
        }

        private void UpdateHierarchyHours(ht hour, double? hoursToAdd)
        {
            Action<HIERARCHY> getAllParents = null;
            IList<ht_h> hierarchies = new List<ht_h>();

            getAllParents = new Action<HIERARCHY>(
               h =>
               {
                   if (h.PARENT_ID != -1 && h.PARENT_ID != null)
                   {
                       var parent = this.DB.HIERARCHies.FirstOrDefault(p => p.HIERARCHY_ID == h.PARENT_ID);
                       if (parent != null)
                       {
                           var h_hour = this.DB.ht_hs.FirstOrDefault(p => p.hierarchy_id == parent.HIERARCHY_ID && p.an == Int32.Parse(hour.year) && p.luna == Int32.Parse(hour.month));
                           if (h_hour != null)
                           {
                               hierarchies.Add(h_hour);
                           }
                           else
                           {
                               ht_h newhour = new ht_h()
                               {
                                   hierarchy_id = parent.HIERARCHY_ID,
                                   an = Convert.ToInt32(hour.year),
                                   luna = Convert.ToInt32(hour.month),
                               };
                               hierarchies.Add(newhour);
                           }
                       }
                       getAllParents(parent);
                   }
               }
            );

            if (hour.PERSON != null)
            {
                getAllParents(hour.PERSON.HIERARCHY);
            }

            string genderMale = this.DB.ConfigDropDownLists.FirstOrDefault(c => c.ddl_id == 6 && c.value == "1").Name_fr;
            string genderFemale = this.DB.ConfigDropDownLists.FirstOrDefault(c => c.ddl_id == 6 && c.value == "2").Name_fr;

            string typeIntern = this.DB.PersonnelTypes.FirstOrDefault(p => p.Code == "01").Name;
            string typeExtern = this.DB.PersonnelTypes.FirstOrDefault(p => p.Code == "02").Name;
            string typeTemp = this.DB.PersonnelTypes.FirstOrDefault(p => p.Code == "03").Name;
            //string typeStagiar = this.DB.PersonnelTypes.FirstOrDefault(p=>p.Code == "04").Name;

            //double hoursToAdd = (double)hour.nr_hours;


            #region First Parent
            var firstParent = hierarchies.FirstOrDefault();
            if (firstParent != null)
            {
                if (hour.PERSON.GENDER == genderMale)
                {
                    if (hour.PERSON.PERSONNEL_TYPE == typeIntern)
                    {
                        if (firstParent.ef_intern_b_total_ore != null)
                        {
                            firstParent.ef_intern_b_total_ore += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_intern_b_total_ore = hoursToAdd;
                        }

                        if (firstParent.ef_intern_b_total_ore_cumul != null)
                        {
                            firstParent.ef_intern_b_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_intern_b_total_ore_cumul = hoursToAdd;
                        }

                        if (firstParent.ef_intern_b_efectiv != null)
                        {
                            firstParent.ef_intern_b_efectiv += 1;
                        }
                        else
                        {
                            firstParent.ef_intern_b_efectiv = 1;
                        }

                        if (firstParent.ef_intern_b_efectiv_cumul != null)
                        {
                            firstParent.ef_intern_b_efectiv_cumul += 1;
                        }
                        else
                        {
                            firstParent.ef_intern_b_efectiv_cumul = 1;
                        }

                        if (firstParent.ef_intern_fb_total_ore != null)
                        {
                            firstParent.ef_intern_fb_total_ore += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_intern_fb_total_ore = hoursToAdd;
                        }

                        if (firstParent.ef_intern_fb_total_ore_cumul != null)
                        {
                            firstParent.ef_intern_fb_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_intern_fb_total_ore_cumul = hoursToAdd;
                        }

                        if (firstParent.ef_intern_fb_efectiv != null)
                        {
                            firstParent.ef_intern_fb_efectiv += 1;
                        }
                        else
                        {
                            firstParent.ef_intern_fb_efectiv = 1;
                        }

                        if (firstParent.ef_intern_fb_efectiv_cumul != null)
                        {
                            firstParent.ef_intern_fb_efectiv_cumul += 1;
                        }
                        else
                        {
                            firstParent.ef_intern_fb_efectiv_cumul = 1;
                        }
                    }
                    else if (hour.PERSON.PERSONNEL_TYPE == typeExtern)
                    {
                        if (firstParent.ef_extern_b_total_ore != null)
                        {
                            firstParent.ef_extern_b_total_ore += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_extern_b_total_ore = hoursToAdd;
                        }

                        if (firstParent.ef_extern_b_total_ore_cumul != null)
                        {
                            firstParent.ef_extern_b_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_extern_b_total_ore_cumul = hoursToAdd;
                        }

                        if (firstParent.ef_extern_b_efectiv != null)
                        {
                            firstParent.ef_extern_b_efectiv += 1;
                        }
                        else
                        {
                            firstParent.ef_extern_b_efectiv = 1;
                        }

                        if (firstParent.ef_extern_b_efectiv_cumul != null)
                        {
                            firstParent.ef_extern_b_efectiv_cumul += 1;
                        }
                        else
                        {
                            firstParent.ef_extern_b_efectiv_cumul = 1;
                        }

                        if (firstParent.ef_extern_fb_total_ore != null)
                        {
                            firstParent.ef_extern_fb_total_ore += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_extern_fb_total_ore = hoursToAdd;
                        }

                        if (firstParent.ef_extern_fb_total_ore_cumul != null)
                        {
                            firstParent.ef_extern_fb_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_extern_fb_total_ore_cumul = hoursToAdd;
                        }

                        if (firstParent.ef_extern_fb_efectiv != null)
                        {
                            firstParent.ef_extern_fb_efectiv += 1;
                        }
                        else
                        {
                            firstParent.ef_extern_fb_efectiv = 1;
                        }

                        if (firstParent.ef_extern_fb_efectiv_cumul != null)
                        {
                            firstParent.ef_extern_fb_efectiv_cumul += 1;
                        }
                        else
                        {
                            firstParent.ef_extern_fb_efectiv_cumul = 1;
                        }
                    }
                    else if (hour.PERSON.PERSONNEL_TYPE == typeTemp)
                    {
                        if (firstParent.ef_interimar_b_total_ore != null)
                        {
                            firstParent.ef_interimar_b_total_ore += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_interimar_b_total_ore = hoursToAdd;
                        }

                        if (firstParent.ef_interimar_b_total_ore_cumul != null)
                        {
                            firstParent.ef_interimar_b_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_interimar_b_total_ore_cumul = hoursToAdd;
                        }

                        if (firstParent.ef_interimar_b_efectiv != null)
                        {
                            firstParent.ef_interimar_b_efectiv += 1;
                        }
                        else
                        {
                            firstParent.ef_interimar_b_efectiv = 1;
                        }

                        if (firstParent.ef_interimar_b_efectiv_cumul != null)
                        {
                            firstParent.ef_interimar_b_efectiv_cumul += 1;
                        }
                        else
                        {
                            firstParent.ef_interimar_b_efectiv_cumul = 1;
                        }

                        if (firstParent.ef_interimar_fb_total_ore != null)
                        {
                            firstParent.ef_interimar_fb_total_ore += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_interimar_fb_total_ore = hoursToAdd;
                        }

                        if (firstParent.ef_interimar_fb_total_ore_cumul != null)
                        {
                            firstParent.ef_interimar_fb_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_interimar_fb_total_ore_cumul = hoursToAdd;
                        }

                        if (firstParent.ef_interimar_fb_efectiv != null)
                        {
                            firstParent.ef_interimar_fb_efectiv += 1;
                        }
                        else
                        {
                            firstParent.ef_interimar_fb_efectiv = 1;
                        }

                        if (firstParent.ef_interimar_fb_efectiv_cumul != null)
                        {
                            firstParent.ef_interimar_fb_efectiv_cumul += 1;
                        }
                        else
                        {
                            firstParent.ef_interimar_fb_efectiv_cumul = 1;
                        }
                    }

                    if (firstParent.ef_global_b_efectiv != null)
                    {
                        firstParent.ef_global_b_efectiv += 1;
                    }
                    else
                    {
                        firstParent.ef_global_b_efectiv = 1;
                    }

                    if (firstParent.ef_global_b_efectiv_cumul != null)
                    {
                        firstParent.ef_global_b_efectiv_cumul += 1;
                    }
                    else
                    {
                        firstParent.ef_global_b_efectiv_cumul = 1;
                    }

                    if (firstParent.ef_global_b_total_ore != null)
                    {
                        firstParent.ef_global_b_total_ore += hoursToAdd;
                    }
                    else
                    {
                        firstParent.ef_global_b_total_ore = hoursToAdd;
                    }

                    if (firstParent.ef_global_b_total_ore_cumul != null)
                    {
                        firstParent.ef_global_b_total_ore_cumul += hoursToAdd;
                    }
                    else
                    {
                        firstParent.ef_global_b_total_ore_cumul = hoursToAdd;
                    }
                    

                    if (firstParent.ef_global_fb_efectiv != null)
                    {
                        firstParent.ef_global_fb_efectiv += 1;
                    }
                    else
                    {
                        firstParent.ef_global_fb_efectiv = 1;
                    }

                    if (firstParent.ef_global_fb_efectiv_cumul != null)
                    {
                        firstParent.ef_global_fb_efectiv_cumul += 1;
                    }
                    else
                    {
                        firstParent.ef_global_fb_efectiv_cumul = 1;
                    }

                    if (firstParent.ef_global_fb_total_ore != null)
                    {
                        firstParent.ef_global_fb_total_ore += hoursToAdd;
                    }
                    else
                    {
                        firstParent.ef_global_fb_total_ore = hoursToAdd;
                    }

                    if (firstParent.ef_global_fb_total_ore_cumul != null)
                    {
                        firstParent.ef_global_fb_total_ore_cumul += hoursToAdd;
                    }
                    else
                    {
                        firstParent.ef_global_fb_total_ore_cumul = hoursToAdd;
                    }
                }
                else if (hour.PERSON.GENDER == genderFemale)
                {
                    if (hour.PERSON.PERSONNEL_TYPE == typeIntern)
                    {
                        if (firstParent.ef_intern_f_total_ore != null)
                        {
                            firstParent.ef_intern_f_total_ore += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_intern_f_total_ore = hoursToAdd;
                        }

                        if (firstParent.ef_intern_f_total_ore_cumul != null)
                        {
                            firstParent.ef_intern_f_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_intern_f_total_ore_cumul = hoursToAdd;
                        }

                        if (firstParent.ef_intern_f_efectiv != null)
                        {
                            firstParent.ef_intern_f_efectiv += 1;
                        }
                        else
                        {
                            firstParent.ef_intern_f_efectiv = 1;
                        }

                        if (firstParent.ef_intern_f_efectiv_cumul != null)
                        {
                            firstParent.ef_intern_f_efectiv_cumul += 1;
                        }
                        else
                        {
                            firstParent.ef_intern_f_efectiv_cumul = 1;
                        }

                        if (firstParent.ef_intern_fb_total_ore != null)
                        {
                            firstParent.ef_intern_fb_total_ore += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_intern_fb_total_ore = hoursToAdd;
                        }

                        if (firstParent.ef_intern_fb_total_ore_cumul != null)
                        {
                            firstParent.ef_intern_fb_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_intern_fb_total_ore_cumul = hoursToAdd;
                        }

                        if (firstParent.ef_intern_fb_efectiv != null)
                        {
                            firstParent.ef_intern_fb_efectiv += 1;
                        }
                        else
                        {
                            firstParent.ef_intern_fb_efectiv = 1;
                        }

                        if (firstParent.ef_intern_fb_efectiv_cumul != null)
                        {
                            firstParent.ef_intern_fb_efectiv_cumul += 1;
                        }
                        else
                        {
                            firstParent.ef_intern_fb_efectiv_cumul = 1;
                        }
                    }
                    else if (hour.PERSON.PERSONNEL_TYPE == typeExtern)
                    {
                        if (firstParent.ef_extern_f_total_ore != null)
                        {
                            firstParent.ef_extern_f_total_ore += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_extern_f_total_ore = hoursToAdd;
                        }

                        if (firstParent.ef_extern_f_total_ore_cumul != null)
                        {
                            firstParent.ef_extern_f_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_extern_f_total_ore_cumul = hoursToAdd;
                        }

                        if (firstParent.ef_extern_f_efectiv != null)
                        {
                            firstParent.ef_extern_f_efectiv += 1;
                        }
                        else
                        {
                            firstParent.ef_extern_f_efectiv = 1;
                        }

                        if (firstParent.ef_extern_f_efectiv_cumul != null)
                        {
                            firstParent.ef_extern_f_efectiv_cumul += 1;
                        }
                        else
                        {
                            firstParent.ef_extern_f_efectiv_cumul = 1;                                
                        }

                        if (firstParent.ef_extern_fb_total_ore != null)
                        {
                            firstParent.ef_extern_fb_total_ore += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_extern_fb_total_ore = hoursToAdd;
                        }

                        if (firstParent.ef_extern_fb_total_ore_cumul != null)
                        {
                            firstParent.ef_extern_fb_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_extern_fb_total_ore_cumul = hoursToAdd;
                        }

                        if (firstParent.ef_extern_fb_efectiv != null)
                        {
                            firstParent.ef_extern_fb_efectiv += 1;
                        }
                        else
                        {
                            firstParent.ef_extern_fb_efectiv = 1;
                        }

                        if (firstParent.ef_extern_fb_efectiv_cumul != null)
                        {
                            firstParent.ef_extern_fb_efectiv_cumul += 1;
                        }
                        else
                        {
                            firstParent.ef_extern_fb_efectiv_cumul = 1;
                        }
                    }
                    else if (hour.PERSON.PERSONNEL_TYPE == typeTemp)
                    {
                        if (firstParent.ef_interimar_f_total_ore != null)
                        {
                            firstParent.ef_interimar_f_total_ore += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_interimar_f_total_ore = hoursToAdd;
                        }

                        if (firstParent.ef_interimar_f_total_ore_cumul != null)
                        {
                            firstParent.ef_interimar_f_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_interimar_f_total_ore_cumul = hoursToAdd;
                        }

                        if (firstParent.ef_interimar_f_efectiv != null)
                        {
                            firstParent.ef_interimar_f_efectiv += 1;
                        }
                        else
                        {
                            firstParent.ef_interimar_f_efectiv = 1;
                        }

                        if (firstParent.ef_interimar_f_efectiv_cumul != null)
                        {
                            firstParent.ef_interimar_f_efectiv_cumul += 1;
                        }
                        else
                        {
                            firstParent.ef_interimar_f_efectiv_cumul = 1;
                        }

                        if (firstParent.ef_interimar_fb_total_ore != null)
                        {
                            firstParent.ef_interimar_fb_total_ore += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_interimar_fb_total_ore = hoursToAdd;
                        }

                        if (firstParent.ef_interimar_fb_total_ore_cumul != null)
                        {
                            firstParent.ef_interimar_fb_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            firstParent.ef_interimar_fb_total_ore_cumul = hoursToAdd;
                        }

                        if (firstParent.ef_interimar_fb_efectiv != null)
                        {
                            firstParent.ef_interimar_fb_efectiv += 1;
                        }
                        else
                        {
                            firstParent.ef_interimar_fb_efectiv = 1;
                        }

                        if (firstParent.ef_interimar_fb_efectiv_cumul != null)
                        {
                            firstParent.ef_interimar_fb_efectiv_cumul += 1;
                        }
                        else
                        {
                            firstParent.ef_interimar_fb_efectiv_cumul = 1;
                        }
                    }
                    if (firstParent.ef_global_f_efectiv != null)
                    {
                        firstParent.ef_global_f_efectiv += 1;
                    }
                    else
                    {
                        firstParent.ef_global_f_efectiv = 1;
                    }

                    if (firstParent.ef_global_f_efectiv_cumul != null)
                    {
                        firstParent.ef_global_f_efectiv_cumul += 1;
                    }
                    else
                    {
                        firstParent.ef_global_f_efectiv_cumul = 1;
                    }

                    if (firstParent.ef_global_f_total_ore != null)
                    {
                        firstParent.ef_global_f_total_ore += hoursToAdd;
                    }
                    else
                    {
                        firstParent.ef_global_f_total_ore = hoursToAdd;
                    }

                    if (firstParent.ef_global_f_total_ore_cumul != null)
                    {
                        firstParent.ef_global_f_total_ore_cumul += hoursToAdd;
                    }
                    else
                    {
                        firstParent.ef_global_f_total_ore_cumul = hoursToAdd;
                    }

                    if (firstParent.ef_global_fb_efectiv != null)
                    {
                        firstParent.ef_global_fb_efectiv += 1;
                    }
                    else
                    {
                        firstParent.ef_global_fb_efectiv = 1;
                    }

                    if (firstParent.ef_global_fb_efectiv_cumul != null)
                    {
                        firstParent.ef_global_fb_efectiv_cumul += 1;
                    }
                    else
                    {
                        firstParent.ef_global_fb_efectiv_cumul = 1;
                    }

                    if (firstParent.ef_global_fb_total_ore != null)
                    {
                        firstParent.ef_global_fb_total_ore += hoursToAdd;
                    }
                    else
                    {
                        firstParent.ef_global_fb_total_ore = hoursToAdd;
                    }

                    if (firstParent.ef_global_fb_total_ore_cumul != null)
                    {
                        firstParent.ef_global_fb_total_ore_cumul += hoursToAdd;
                    }
                    else
                    {
                        firstParent.ef_global_fb_total_ore_cumul = hoursToAdd;
                    }
                }
            }

            if (firstParent.ht_h_id == 0)
            {
                this.DB.ht_hs.InsertOnSubmit(firstParent);
            }
            #endregion

            #region Other Parents
            foreach (var item in hierarchies.Skip(1))
            {
                if (hour.PERSON.GENDER == genderMale)
                {
                    if (hour.PERSON.PERSONNEL_TYPE == typeIntern)
                    {

                        if (item.ef_intern_b_total_ore_cumul != null)
                        {
                            item.ef_intern_b_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            item.ef_intern_b_total_ore_cumul = hoursToAdd;
                        }

                        if (item.ef_intern_b_efectiv_cumul != null)
                        {
                            item.ef_intern_b_efectiv_cumul += 1;
                        }
                        else
                        {
                            item.ef_intern_b_efectiv_cumul = 1;
                        }

                        if (item.ef_intern_fb_total_ore_cumul != null)
                        {
                            item.ef_intern_fb_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            item.ef_intern_fb_total_ore_cumul = hoursToAdd;
                        }

                        if (item.ef_intern_fb_efectiv_cumul != null)
                        {
                            item.ef_intern_fb_efectiv_cumul += 1;
                        }
                        else
                        {
                            item.ef_intern_fb_efectiv_cumul = 1;
                        }
                    }
                    else if (hour.PERSON.PERSONNEL_TYPE == typeExtern)
                    {

                        if (item.ef_extern_b_total_ore_cumul != null)
                        {
                            item.ef_extern_b_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            item.ef_extern_b_total_ore_cumul = hoursToAdd;
                        }

                        if (item.ef_extern_b_efectiv_cumul != null)
                        {
                            item.ef_extern_b_efectiv_cumul += 1;
                        }
                        else
                        {
                            item.ef_extern_b_efectiv_cumul = 1;
                        }

                        if (item.ef_extern_fb_total_ore_cumul != null)
                        {
                            item.ef_extern_fb_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            item.ef_extern_fb_total_ore_cumul = hoursToAdd;
                        }

                        if (item.ef_extern_fb_efectiv_cumul != null)
                        {
                            item.ef_extern_fb_efectiv_cumul += 1;
                        }
                        else
                        {
                            item.ef_extern_fb_efectiv_cumul = 1;
                        }
                    }
                    else if (hour.PERSON.PERSONNEL_TYPE == typeTemp)
                    {

                        if (item.ef_interimar_b_total_ore_cumul != null)
                        {
                            item.ef_interimar_b_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            item.ef_interimar_b_total_ore_cumul = hoursToAdd;
                        }

                        if (item.ef_interimar_b_efectiv_cumul != null)
                        {
                            item.ef_interimar_b_efectiv_cumul += 1;
                        }
                        else
                        {
                            item.ef_interimar_b_efectiv_cumul = 1;
                        }

                        if (item.ef_interimar_fb_total_ore_cumul != null)
                        {
                            item.ef_interimar_fb_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            item.ef_interimar_fb_total_ore_cumul = hoursToAdd;
                        }

                        if (item.ef_interimar_fb_efectiv_cumul != null)
                        {
                            item.ef_interimar_fb_efectiv_cumul += 1;
                        }
                        else
                        {
                            item.ef_interimar_fb_efectiv_cumul = 1;
                        }
                    }

                    if (item.ef_global_b_efectiv_cumul != null)
                    {
                        item.ef_global_b_efectiv_cumul += 1;
                    }
                    else
                    {
                        item.ef_global_b_efectiv_cumul = 1;
                    }

                    if (item.ef_global_b_total_ore_cumul != null)
                    {
                        item.ef_global_b_total_ore_cumul += hoursToAdd;
                    }
                    else
                    {
                        item.ef_global_b_total_ore_cumul = hoursToAdd;
                    }

                    if (item.ef_global_fb_efectiv_cumul != null)
                    {
                        item.ef_global_fb_efectiv_cumul += 1;
                    }
                    else
                    {
                        item.ef_global_fb_efectiv_cumul = 1;
                    }

                    if (item.ef_global_fb_total_ore_cumul != null)
                    {
                        item.ef_global_fb_total_ore_cumul += hoursToAdd;
                    }
                    else
                    {
                        item.ef_global_fb_total_ore_cumul = hoursToAdd;
                    }
                }
                else if (hour.PERSON.GENDER == genderFemale)
                {
                    if (hour.PERSON.PERSONNEL_TYPE == typeIntern)
                    {

                        if (item.ef_intern_f_total_ore_cumul != null)
                        {
                            item.ef_intern_f_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            item.ef_intern_f_total_ore_cumul = hoursToAdd;
                        }

                        if (item.ef_intern_f_efectiv_cumul != null)
                        {
                            item.ef_intern_f_efectiv_cumul += 1;
                        }
                        else
                        {
                            item.ef_intern_f_efectiv_cumul = 1;
                        }

                        if (item.ef_intern_fb_total_ore_cumul != null)
                        {
                            item.ef_intern_fb_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            item.ef_intern_fb_total_ore_cumul = hoursToAdd;
                        }

                        if (item.ef_intern_fb_efectiv_cumul != null)
                        {
                            item.ef_intern_fb_efectiv_cumul += 1;
                        }
                        else
                        {
                            item.ef_intern_fb_efectiv_cumul = 1;
                        }
                    }
                    else if (hour.PERSON.PERSONNEL_TYPE == typeExtern)
                    {

                        if (item.ef_extern_f_total_ore_cumul != null)
                        {
                            item.ef_extern_f_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            item.ef_extern_f_total_ore_cumul = hoursToAdd;
                        }
                        if (item.ef_extern_f_efectiv_cumul != null)
                        {
                            item.ef_extern_f_efectiv_cumul += 1;
                        }
                        else
                        {
                            item.ef_extern_f_efectiv_cumul = 1;
                        }

                        if (item.ef_extern_fb_total_ore_cumul != null)
                        {
                            item.ef_extern_fb_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            item.ef_extern_fb_total_ore_cumul = hoursToAdd;
                        }

                        if (item.ef_extern_fb_efectiv_cumul != null)
                        {
                            item.ef_extern_fb_efectiv_cumul += 1;
                        }
                        else
                        {
                            item.ef_extern_fb_efectiv_cumul = 1;
                        }
                    }
                    else if (hour.PERSON.PERSONNEL_TYPE == typeTemp)
                    {

                        if (item.ef_interimar_f_total_ore_cumul != null)
                        {
                            item.ef_interimar_f_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            item.ef_interimar_f_total_ore_cumul = hoursToAdd;
                        }

                        if (item.ef_interimar_f_efectiv_cumul != null)
                        {
                            item.ef_interimar_f_efectiv_cumul += 1;
                        }
                        else
                        {
                            item.ef_interimar_f_efectiv_cumul = 1;
                        }

                        if (item.ef_interimar_fb_total_ore_cumul != null)
                        {
                            item.ef_interimar_fb_total_ore_cumul += hoursToAdd;
                        }
                        else
                        {
                            item.ef_interimar_fb_total_ore_cumul = hoursToAdd;
                        }

                        if (item.ef_interimar_fb_efectiv_cumul != null)
                        {
                            item.ef_interimar_fb_efectiv_cumul += 1;
                        }
                        else
                        {
                            item.ef_interimar_fb_efectiv_cumul = 1;
                        }
                    }

                    if (item.ef_global_f_efectiv_cumul != null)
                    {
                        item.ef_global_f_efectiv_cumul += 1;
                    }
                    else
                    {
                        item.ef_global_f_efectiv_cumul = 1;
                    }

                    if (item.ef_global_f_total_ore_cumul != null)
                    {
                        item.ef_global_f_total_ore_cumul += hoursToAdd;
                    }
                    else
                    {
                        item.ef_global_f_total_ore_cumul = hoursToAdd;
                    }

                    if (item.ef_global_fb_efectiv_cumul != null)
                    {
                        item.ef_global_fb_efectiv_cumul += 1;
                    }
                    else
                    {
                        item.ef_global_fb_efectiv_cumul = 1;
                    }

                    if (item.ef_global_fb_total_ore_cumul != null)
                    {
                        item.ef_global_fb_total_ore_cumul += hoursToAdd;
                    }
                    else
                    {
                        item.ef_global_fb_total_ore_cumul = hoursToAdd;
                    }
                }
                if (item.ht_h_id == 0)
                {
                    this.DB.ht_hs.InsertOnSubmit(item);
                }
            }
            #endregion
            this.DB.SubmitChanges();
        }
    }
}